package com.codemonkey.lq.extend.springmvc;

import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;

public abstract class AbstractFileUploadInterceptor implements FileUploadInterceptor {

	@Override
	public abstract void doUpload(Map<String, Object> context);

	@Override
	public boolean preUpload(Map<String, Object> context){
		return true;
	};

	@Override
	public String convertFileFinalName(Map<String, Object> context) {
		final String orginFileName = context.get(FileUploadInterceptor.CONTEXT_KEY_FILENAME_ORIGIN)
				.toString();
		return AbstractFileUploadInterceptor.changeFilename2UUID(orginFileName);
	}

	/**
	 * 将文件名转变为UUID命名的 ,保留文件后缀
	 * 
	 * @param filename
	 * @return
	 */
	static String changeFilename2UUID(String filename) {
		String uuid = UUID.randomUUID().toString();
		return uuid + "." + FilenameUtils.getExtension(filename);
	}
}
